﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace WqHtmlAnchorSpider
{
    public class DbManager
    {
        private wqspider204Entities db;


        public DbManager()
        {
            db = new wqspider204Entities();
        }

        public void AddAnchor(AnchorObj anchor)
        {
            if (anchor == null)
            {
                return;
            }

            db.anchors.Add(anchor.GetDbAnchor());
            db.SaveChanges();
        }

        public void AddAnchors(List<AnchorObj> anchorObjs)
        {
            if (anchorObjs == null || anchorObjs.Count == 0)
            {
                return;
            }

            foreach (var anchorObj in anchorObjs)
            {
                db.anchors.Add(anchorObj.GetDbAnchor());
            }

            db.SaveChanges();
        }

        public List<AnchorObj> FindAnchorObjsByName(string key)
        {
            List<AnchorObj> objs = new List<AnchorObj>();
            var anchors = from anchorObj in db.anchors where anchorObj.name.Contains(key) select anchorObj;
            foreach (var anchor in anchors)
            {
                objs.Add(new AnchorObj(anchor));
            }
            return objs;
        }
    }
}